package com.funo.business.ctrler;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.funo.business.entities.Product;
import com.funo.business.entities.ProductItem;
import com.funo.business.service.ProductService;
import com.funo.common.factory.JsonMsgFactory;
import com.funo.common.pojo.PageResult;
import com.funo.common.pojo.QueryCondition;
import com.funo.common.utils.Constant;
import com.funo.core.base.BaseCtrler;
import com.funo.main.entities.User;

@Controller
@RequestMapping("/product")
public class ProductCtrler extends BaseCtrler{
	@Autowired
	@Qualifier("productService")
	private ProductService productService;
	
	@RequestMapping(value="/addProduct",method=RequestMethod.POST)
	@ResponseBody
	public String addProduct(Product product){
		if (product!=null) {
			try {
				Integer type = productService.saveProduct(product);
				if (type == Constant.STATUS_SUSSESS) {//成功
					return toJson(JsonMsgFactory.successMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
				return toJson(JsonMsgFactory.failMsg());
			}
		}
		return null;
	}
	
	@RequestMapping(value="/editProduct",method=RequestMethod.POST)
	@ResponseBody
	public String editProduct(Product product){
		try {
			Integer type = productService.updateProduct(product);
			if (type == Constant.STATUS_SUSSESS) {//成功
				return toJson(JsonMsgFactory.successMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return toJson(JsonMsgFactory.failMsg());
		}
		return null;
	}
	
	@RequestMapping(value="/delProduct",method=RequestMethod.POST)
	public @ResponseBody String delProduct(String deleteIds){
		try {

			String[]Ids=deleteIds.split(",");//传递到数据库的参数

			for (int i = 0; i < Ids.length; i++) {
				String id=Ids[i];
				productService.delProduct(id);
			}
			return toJson(JsonMsgFactory.successMsg());
		} catch (Exception e) {
			return toJson(JsonMsgFactory.failMsg());
		}
	}
	
	@RequestMapping(value="/loadProduct",method=RequestMethod.POST)
	public @ResponseBody String loadProduct(String productId){
		return toJson(productService.getProduct(productId));
	}
	
	@RequestMapping(value="/addItem",method=RequestMethod.POST)
	@ResponseBody
	public String addItem(ProductItem item){
		if (item!=null) {
			try {
				Integer type = productService.saveProductItem(item);
				if (type == Constant.STATUS_SUSSESS) {//成功
					return toJson(JsonMsgFactory.successMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
				return toJson(JsonMsgFactory.failMsg());
			}
		}
		return null;
	}
	
	@RequestMapping(value="/editItem",method=RequestMethod.POST)
	@ResponseBody
	public String editItem(ProductItem item){
		try {
			Integer type = productService.updateProductItem(item);
			if (type == Constant.STATUS_SUSSESS) {//成功
				return toJson(JsonMsgFactory.successMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return toJson(JsonMsgFactory.failMsg());
		}
		return null;
	}
	
	@RequestMapping(value="/delItem",method=RequestMethod.POST)
	public @ResponseBody String delItem(String deleteIds){
		try {

			String[]Ids=deleteIds.split(",");//传递到数据库的参数

			for (int i = 0; i < Ids.length; i++) {
				String id=Ids[i];
				productService.delProductItem(id);
			}
			return toJson(JsonMsgFactory.successMsg());
		} catch (Exception e) {
			return toJson(JsonMsgFactory.failMsg());
		}
	}
	
	@RequestMapping(value="/loadItem",method=RequestMethod.POST)
	public @ResponseBody String loadItem(String productItemId){
		return toJson(productService.getProductItem(productItemId));
	}
	
	@RequestMapping(value="/getItemsByUser")
	public String getItemsByUser(HttpSession session){
		User user = sessionUser(session);
		if (user==null || user.getId()==null) {
			return toJson(JsonMsgFactory.fail("请先登录,再进行操作!"));
		}
		List<ProductItem> list = null;
		try {
			list = productService.getItemsByUser(user.getId());
			return toJson(list);
		} catch (Exception e) {
			return toJson(JsonMsgFactory.failMsg());
		}
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public String page(QueryCondition queryCondition,Product product){
		PageResult<Product> page = productService.listPage(queryCondition,product);
		return toJson(page);
	}
	
	@Override
	protected String handlerException(Exception e) {
		e.printStackTrace();
		System.err.println("[handlerException:MemberController]异常类型："+e.getClass().getSimpleName());
		System.err.println(e.getMessage());
		return "系统异常";
	}
}
